﻿#include "Mouse.h"


namespace Base {
	MouseEvent::MouseEvent(Type type, const Mouse* mouse) :
		type(type),
		leftIsPressed(mouse->leftIsPressed),
		rightIsPressed(mouse->rightIsPressed),
		x(mouse->x),
		y(mouse->y)
	{

	}

	//--------------------------------------------
	// Mouse 实现
	std::optional<MouseEvent> Mouse::Read() {
		while (m_buffer.size() > 0)
		{
			auto e = m_buffer.front();
			m_buffer.pop();
			return e;
		}
		return {};
	}


	void Mouse::OnMouseMove(int newx, int newy) {
		x = newx;
		y = newy;
		m_buffer.emplace(MouseEvent::Type::Move, this);
		TrimBuffer();
	}
	void Mouse::OnMouseLeave() {
		isInWindow = false;
		m_buffer.emplace(MouseEvent::Type::Leave, this);
		TrimBuffer();
	}
	void Mouse::OnMouseEnter() {
		isInWindow = true;
		m_buffer.emplace(MouseEvent::Type::Enter, this);
		TrimBuffer();
	}
	void Mouse::OnLeftPressed(int newx, int newy) {
		x = newx;
		y = newy;
		leftIsPressed = true;
		m_buffer.emplace(MouseEvent::Type::LPress, this);
		TrimBuffer();
	}
	void Mouse::OnLeftReleased(int newx, int newy) {
		x = newx;
		y = newy;
		leftIsPressed = false;
		m_buffer.emplace(MouseEvent::Type::LRelease, this);
		TrimBuffer();
	}
	void Mouse::OnRightPressed(int newx, int newy) {
		x = newx;
		y = newy;
		rightIsPressed = true;
		m_buffer.emplace(MouseEvent::Type::RPress, this);
		TrimBuffer();
	}
	void Mouse::OnRightReleased(int newx, int newy) {
		x = newx;
		y = newy;
		rightIsPressed = false;
		m_buffer.emplace(MouseEvent::Type::RRelease, this);
		TrimBuffer();
	}

	//void Mouse::OnWheelUp(int newx, int newy) {
	//	x = newx;
	//	y = newy;
	//	m_buffer.emplace(MouseEvent::Type::WheelUp, this);
	//	TrimBuffer();
	//}
	//void Mouse::OnWheelDown(int newx, int newy) {
	//	x = newx;
	//	y = newy;
	//	m_buffer.emplace(MouseEvent::Type::WheelDown, this);
	//	TrimBuffer();
	//}

	//void Mouse::OnWheelDelta(int x, int y, int delta) {
	//	wheelDeltaCarry += delta;
	//}

	void Mouse::TrimBuffer() {
		while (m_buffer.size() > kBufferSize)
		{
			m_buffer.pop();
		}
	}
}